<?php
declare (strict_types=1);

namespace app\model\home;

use app\common\model\BaseModel;
use \libs\Log;

class PropMember extends BaseModel
{
    protected $pk = 'id';

    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @param $value
     * @return Prop|array|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 道具信息
     */
    protected function getPropNameAttr($value)
    {
        return Prop::where('id', $value)->value('prop_name');
    }

    /**
     * @param $value
     * @return mixed
     * 获取道具封面
     */
    protected function getPropCoverAttr($value)
    {
        return Prop::where('id', $value)->value('prop_cover');
    }

    /**
     * @param $value
     * @return mixed
     * 获取道具类型
     */
    protected function getTypeAttr($value)
    {
        return Prop::where('id', $value)->value('type');
    }

    /**
     * 获得列表
     * @param bool $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($where = true, $fields = '*', $order = null, $page = 1, $pageSize = 20)
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
        $count = $this->where($where)->group('prop_id')->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;
        $list = $this->where($where)
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->group('prop_id')
            ->select();
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->returnRight($data);
    }
}
